var Pool = require('../Tools/TestCenterPool').pool;
var execTrans = require('../Tools/TestCenterPool').execTrans;
var _ = require('lodash');

class ProjectModel {
  async insert(project) {
    try {
      let project_sql = `INSERT INTO test_project (name, description,ding_webhook,real_name) values ('${project.name}', ?,?,'${project.realname}')`;
      let sql_params = new Array(2);
      sql_params[0] = project.description ? project.description : '';
      sql_params[1] = project.ding? project.ding :'';

      let result = await Pool.queryAsync(project_sql, sql_params);
      return result.affectedRows == 1 ? result.insertId : 0;
    } catch (error) {
      console.log(error)
      throw error;
    }
  }

  async update(project) {
    try {
      let project_sql = `UPDATE test_project SET  `;
      let record_sql = '';
      if (project.name) {
        project_sql += `name='${project.name}',`;
        record_sql += `UPDATE test_record SET project_name='${project.name}' WHERE project_id=${project.id} `;
      }
      if (project.description) {
        project_sql += `description='${project.description}',`;
      }
      if(project.ding){
        project_sql += `ding_webhook='${project.ding}',`;
      }
      project_sql = project_sql.substring(0, project_sql.lastIndexOf(','));
      project_sql += ` WHERE id=${project.id}`;

      let sqlParams = [];
      sqlParams.push({sql: project_sql, params:[]});
      if(record_sql !== ''){
        sqlParams.push({sql: record_sql, params:[]});
      }
      await execTrans(sqlParams);
      return true;
    } catch (error) {
      throw error;
    }
  }

  async deleteById(id) {
    try {
      let project_sql = `DELETE FROM test_project WHERE id=${id}`;
      let re_project_user_sql = `DELETE FROM test_re_project_user WHERE project_id=${id}`;
      let version_sql = `DELETE FROM test_version WHERE project_id=${id}`;
      var sqlParams = [];
      sqlParams.push({sql:project_sql});
      sqlParams.push({sql:re_project_user_sql});
      sqlParams.push({sql:version_sql});
      await execTrans(sqlParams);
      return true;
    } catch (error) {
      console.log(error);
      throw error;
    }
  }

  async list() {
    let sql = "SELECT id, name, description, modify_time,ding_webhook FROM test_project where 1=1";
    sql += " ORDER BY create_time DESC";
    try {
      let rows = await Pool.queryAsync(sql);
      let projects = rows.map((row) => {
        return {
          id: row.id,
          name: row.name,
          description: row.description,
          modifyTime: row.modify_time,
          ding:row.ding_webhook? row.ding_webhook:null
        }
      })
      return projects;
    } catch (error) {
      throw error;
    }
  }

  async queryById(id) {
    try {
      let project_sql = `SELECT id,name,description,ding_webhook FROM test_project where id=${id}`;
      let project = await Pool.queryAsync(project_sql);
      project = project.map((p) => {
        return {
          id: p.id,
          name: p.name,
          description: p.description,
          ding:p.ding_webhook? p.ding_webhook:null
        }
      })[0];
      return project;
    } catch (error) {
      throw error;
    }
  }

  async getWebhook(name){
    try{
      let webhook_sql = `SELECT ding_webhook FROM test_project where name = '${name}'`;
      let webhook = await Pool.queryAsync(webhook_sql);
      webhook = webhook[0].ding_webhook

      return webhook;
    }catch(error){
      throw error;
    }
  }

  async checkNameExit(project){
    try{
      let sql = `SELECT COUNT(*) AS count FROM test_project where name = '${project.name}' AND id =${project.id} `;
      let result = await Pool.queryAsync(sql);

      let count = result[0].count
      if(count==0){
        let sql1 = `SELECT COUNT(*) AS count FROM test_project where name = '${project.name}'`;
        let result1 = await Pool.queryAsync(sql1);
        if(result1[0].count==0){
          return true
        }else{
          return false
        }
      }else{
        return true
      }
    }catch(error){
      throw error
    }
  }
}

module.exports = new ProjectModel();